package com.woniuxy.controller;

import com.woniuxy.dao.DriverMapper;
import com.woniuxy.pojo.Dictionary;
import com.woniuxy.pojo.Driver;
import com.woniuxy.service.DriverService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.DriverVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class DriverController extends BaseController{

    Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DriverService driverService;
    @Autowired
    private DriverMapper driverMapper;
    @GetMapping("/drivers")
    @ResponseBody
    public DataGridView selectDriversByPage(DriverVo driverVo){
        return driverService.getDriverListByPage(driverVo);
    }
    @RequestMapping(value = "/toDriverManager.do")
    public String toDriverManager(){
        return "driver/driverList";
    }
    @RequestMapping(value = "/toDriverAddPage.do")
    public String toDriverAddPage(){
        return "driver/driverAdd";
    }
    @RequestMapping(value = "/toDriverEditPage.do")
    public String toDriverEditPage(){
        return "driver/driverEdit";
    }

    @PostMapping(value = "/drivers.do")
    @ResponseBody
    public ResultDto addDriver(DriverVo driverVo){
        ResultDto dto=new ResultDto();
        logger.debug("手机号:"+driverVo.getPhone());
        logger.debug("地址:"+driverVo.getAddress());
        logger.debug("姓名:"+driverVo.getName());
        logger.debug("生日:"+driverVo.getBirthday());
        logger.debug("身份证:"+driverVo.getCard());
        logger.debug("备注；"+driverVo.getRemarks());
        logger.debug("状态:"+driverVo.getAble());
        logger.debug("性别:"+driverVo.getGender());
        logger.debug("驾照ID:"+driverVo.getDriver_no());
        logger.debug("部门:"+driverVo.getDept_id());
        Driver driverResult=this.driverService.findDriverByPhone(driverVo.getPhone());
        if(driverResult!=null){
            return ResultDto.DRIVER_EXITS;
        }else{
            boolean flag=this.driverService.addDriver(driverVo);
            if(flag){
                return ResultDto.DRIVER_ADD_SUCCESS;
            }else{
                return ResultDto.DRIVER_ADD_FAILURE;
            }
        }
    }
    @RequestMapping(value = "/toDriverDetailPage.do")
    public String toPeccancyRecorDetaillPage() {
        return "driver/driverDetail";
    }

    @PostMapping("/editdrivers.do")
    @ResponseBody
    public ResultDto upadteDriver(Driver driver){
        try{
            boolean flag=driverService.update(driver);
            if(flag){
                return ResultDto.UPDATE_DRIVER_SUCCESS;
            }else{
                return ResultDto.UPDATE_DRIVER_FAILURE;
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.UPDATE_DRIVER_FAILURE;
        }
    }
    @GetMapping(value = "/driver/{id}")
    @ResponseBody
    public ResultDto deleteDriver(@PathVariable("id") int id){
        try{
            this.driverService.removeDriver(id);
            return ResultDto.DELETE_DRIVER_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.DELETE_DRIVER_FAILURE;
        }
    }

    @RequestMapping(value = "/driver/deleteBatch")
    @ResponseBody
    public ResultDto deleteBathDriver(@RequestParam("ids[]") Integer[] ids){
        try{
            this.driverService.removeBathDriver(ids);
            return ResultDto.DELETE_DRIVER_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.DELETE_DRIVER_FAILURE;
        }
    }

    @GetMapping(value = "/driver/depts")
    @ResponseBody
    public List<Dictionary> depts(){
        return driverMapper.selectAllDepts();
    }

    @GetMapping(value = "/driver/drivertypes")
    @ResponseBody
    public List<Dictionary> drivertypes(){
        return driverMapper.selectAllDriverTypes();
    }
}
